#include <config.h>
#include <core.h>

#define   CURR_WORK_DIR_SIZE 1024

//extern char ** copy_argv;
//char  curr_work_dir[CURR_WORK_DIR_SIZE] = {'\0'};

#if 0
static msgd_int_t get_exe_dir(char *buf,size_t size,const char *argv0);

static msgd_int_t get_exe_dir(char *buf,size_t size,const char *argv0)
{
  if(buf == NULL || size <= 1 || argv0 == NULL) return MSGD_ERROR;
  char *path = (char*)argv0;

  char *lastslash = strrchr(path,'/');

  if(!lastslash){
    char link[512] = {'\0'};
    if(readlink("/proc/self/exe",link,512) == -1){
       ERROR("readlink error");
       return MSGD_ERROR;
    }
    path = link;
    lastslash = strrchr(path,'/');
  }

  if(*path == '.'){

    if(getcwd(buf,size) == NULL){
       ERROR("getcwd failed");
       return MSGD_ERROR;
    }

    const char *p = path + 1; // skip dot
    size_t len = lastslash - p + 1; // include the lastslash
    size_t pos = strlen(buf);
    memcpy(buf+pos,p,len);
  }else{
    size_t len = lastslash - path + 1;
    memcpy(buf,path,len);
  }

  return MSGD_OK;
}
#endif

//extern msgd_global_t global;
msgd_int_t init_global()
{
   DEBUG("start init_global");
   //char    libfilename[1024] = {'\0'};
   global.processes_n  = 1;
   global.worker_threads_n    = 1;
   global.connections_n = 10000;

   queue_init(&global.reusable_connections_queue);
   
   global.pool = create_pool(1024*1024);

   msgd_listening_t *ls,*prev = NULL; 

   #if 0
   LIB_HANDLER  handler;
   char         *err,*work_dir;
  
   work_dir = pool_palloc(global.pool,CURR_WORK_DIR_SIZE);
   if(get_exe_dir(work_dir,CURR_WORK_DIR_SIZE,copy_argv[0]) == MSGD_ERROR){
      return MSGD_ERROR;
   }

   INFO("work_dir:%s  %s",work_dir,copy_argv[0]);
   #endif 

   char* addrs[][4]  = {{"127.0.0.1","8181","json","http"} , {NULL,NULL,NULL,NULL}}; 
   int i; 
   for(i=0; addrs[i][0]; i++){
     u_char *host = (u_char*)addrs[i][0];
     u_char *port = (u_char*)addrs[i][1];
     //u_char *outFormat = (u_char*)addrs[i][2];
     //u_char *protocol  = (u_char*)addrs[i][3];
    
     struct sockaddr_in *serv_addr = pool_calloc(global.pool,sizeof(struct sockaddr_in));
     if(serv_addr == NULL){
       ERROR("memory not enough"); 
       return MSGD_ERROR;
     }
     serv_addr->sin_family = AF_INET;
     if(inet_pton(AF_INET,(char*)host,(void*)&serv_addr->sin_addr) <=0){
        ERROR("inet_pton failed host:%s",host);
        return MSGD_ERROR;
     }
     serv_addr->sin_port         = htons(atol((char*)port));

     ls= pool_calloc(global.pool,sizeof(msgd_listening_t));
     if(ls == NULL){
       ERROR("memory not enough"); 
       return MSGD_ERROR;
     }

     ls->next = prev;
     ls->fd   = -1;
     ls->sockaddr = (struct sockaddr*)serv_addr;
     ls->socklen  = sizeof(*serv_addr);

     ls->addr_text_max_len = strlen((char*)host);
     size_t plen = strlen((char*)port);
     
     host  = pool_calloc(global.pool, ls->addr_text_max_len+1 + plen+1);
     if(host == NULL){
       ERROR("memory not enough"); 
       return MSGD_ERROR;
     }
     port  = cpystrn(host,(u_char*)addrs[i][0],ls->addr_text_max_len + 1) + 1;
     cpystrn(port,(u_char*)addrs[i][1],plen+1);
    
     ls->addr_text = host; 
     ls->port_text = port; 

     ls->backlog = 1024;
     ls->rcvbuf  = -1;
     ls->sndbuf  = -1;

     #if 0
     snprintf(libfilename,1024,"%s"LIB_FILE_NAME_FORMAT,work_dir,protocol);
     handler = LOAD_LIBRARY(libfilename,LOAD_LIBRARY_FLAGS);
     if(!handler){
        ERROR("load_library error:%s",OPEN_LIBRARY_ERROR);
        return MSGD_ERROR;
     }
     //ls->handler = msgd_init_connection;
     OPEN_LIBRARY_ERROR;
     ls->handler = GET_PROC_ADDRESS(handler,LIB_INIT_CONNECTION_FUN);
     if((err=OPEN_LIBRARY_ERROR) != NULL){
       ERROR("get proc address failed %s",err);
       return MSGD_ERROR;
     }

     //FREE_LIBRARY(handler);
     #endif
  
     ls->handler = msgd_init_connection;

     prev = ls;
    
     DEBUG("open listenning sockets host:%s port:%s %p",host,port,ls->handler);
     if(msgd_open_listening_socket(ls) == MSGD_ERROR){
        ERROR("open listening failed host:%s port:%s",host,port);
        return MSGD_ERROR;
     }
   }

   global.listenings = ls;
   global.listenings_n = i;

   DEBUG("end init_global");
   return MSGD_OK;
}
